﻿using System;
using System.Globalization;
using System.IO;

namespace Vosc.Coff
{
	// Token: 0x02000015 RID: 21
	public class CoffParser
	{
		// Token: 0x06000087 RID: 135 RVA: 0x00009FA6 File Offset: 0x000081A6
		private static CoffParser Open(Stream stream)
		{
			return new CoffParser(stream);
		}

		// Token: 0x06000088 RID: 136 RVA: 0x00009FAE File Offset: 0x000081AE
		private static CoffParser Open(string filename)
		{
			return CoffParser.Open(new FileStream(filename, FileMode.Open, FileAccess.Read));
		}

		// Token: 0x06000089 RID: 137 RVA: 0x00009FBD File Offset: 0x000081BD
		private CoffParser(Stream stream)
		{
			this.stream = stream;
		}

		// Token: 0x0600008A RID: 138 RVA: 0x00009FE4 File Offset: 0x000081E4
		private void Close()
		{
			if (this.stream != null)
			{
				this.stream.Close();
				this.stream = null;
			}
		}

		// Token: 0x0600008B RID: 139 RVA: 0x0000A000 File Offset: 0x00008200
		public static Coffile GetCoffile(string fileName)
		{
			if (!File.Exists(fileName))
			{
				return null;
			}
			try
			{
				if (fileName.EndsWith(".out", true, null))
				{
					string text = null;
					text = Out2Hex.Convert(fileName);
					if (string.IsNullOrEmpty(text))
					{
						return null;
					}
					CoffParser coffParser = CoffParser.Open(text);
					try
					{
						long num = coffParser.ReadEntryPoint();
						if (num < 0L)
						{
							return null;
						}
						Coffile coffile = new Coffile((uint)num);
						for (;;)
						{
							CoffSection coffSection = coffParser.ReadNextSection();
							if (coffSection == null)
							{
								break;
							}
							coffile.AddSection(coffSection);
							if (coffSection.Size == 0)
							{
								goto Block_10;
							}
						}
						return null;
						Block_10:
						return coffile;
					}
					finally
					{
						coffParser.Close();
						if (!string.IsNullOrEmpty(text) && File.Exists(text))
						{
							File.Delete(text);
						}
					}
				}
				if (fileName.EndsWith(".bin", true, null))
				{
					return null;
				}
				return null;
			}
			catch
			{
			}
			return null;
		}

		// Token: 0x0600008C RID: 140 RVA: 0x0000A0E0 File Offset: 0x000082E0
		private long ReadEntryPoint()
		{
			for (int i = 0; i < 18; i++)
			{
				int num = this.ReadNextByte(this.stream);
				if (num < 0)
				{
					return -1L;
				}
			}
			return this.ReadNextDword(this.stream);
		}

		// Token: 0x0600008D RID: 141 RVA: 0x0000A11C File Offset: 0x0000831C
		private CoffSection ReadNextSection()
		{
			int num = this.ReadNextWord(this.stream);
			if (num < 0)
			{
				return null;
			}
			if (num == 0)
			{
				return new CoffSection(0, 0u);
			}
			long num2 = this.ReadNextDword(this.stream);
			if (num2 < 0L)
			{
				return null;
			}
			CoffSection coffSection = new CoffSection((ushort)num, (uint)num2);
			for (int i = 0; i < num; i++)
			{
				int num3 = this.ReadNextWord(this.stream);
				if (num3 < 0)
				{
					return null;
				}
				coffSection.WriteWord((ushort)num3);
			}
			return coffSection;
		}

		// Token: 0x0600008E RID: 142 RVA: 0x0000A194 File Offset: 0x00008394
		private long ReadNextDword(Stream stream)
		{
			for (int i = 0; i < 4; i++)
			{
				int num = this.ReadNextByte(stream);
				if (num < 0)
				{
					return -1L;
				}
				this.bytArray[i] = (byte)num;
			}
			return (long)((int)(this.bytArray[0] & byte.MaxValue) << 16 | (int)(this.bytArray[1] & byte.MaxValue) << 24 | (int)(this.bytArray[2] & byte.MaxValue) | (int)(this.bytArray[3] & byte.MaxValue) << 8) & -1;
		}

		// Token: 0x0600008F RID: 143 RVA: 0x0000A20D File Offset: 0x0000840D
		private int ReadNextWord()
		{
			return this.ReadNextWord(this.stream);
		}

		// Token: 0x06000090 RID: 144 RVA: 0x0000A21C File Offset: 0x0000841C
		private int ReadNextWord(Stream stream)
		{
			for (int i = 0; i < 2; i++)
			{
				int num = this.ReadNextByte(stream);
				if (num < 0)
				{
					return -1;
				}
				this.bytArray[i] = (byte)num;
			}
			return ((int)(this.bytArray[0] & byte.MaxValue) | (int)(this.bytArray[1] & byte.MaxValue) << 8) & 65535;
		}

		// Token: 0x06000091 RID: 145 RVA: 0x0000A272 File Offset: 0x00008472
		private int ReadNextByte()
		{
			return this.ReadNextByte(this.stream);
		}

		// Token: 0x06000092 RID: 146 RVA: 0x0000A280 File Offset: 0x00008480
		private int ReadNextByte(Stream stream)
		{
			int i = 0;
			while (i < 2)
			{
				int num = stream.ReadByte();
				if (num < 0)
				{
					return -1;
				}
				if ((num >= 48 && num <= 57) || (num >= 65 && num <= 90) || (num >= 97 && num <= 122))
				{
					this.chrArray[i] = (char)num;
					i++;
				}
			}
			return (int)byte.Parse(new string(this.chrArray), NumberStyles.HexNumber);
		}

		// Token: 0x040000B8 RID: 184
		private Stream stream;

		// Token: 0x040000B9 RID: 185
		private char[] chrArray = new char[2];

		// Token: 0x040000BA RID: 186
		private byte[] bytArray = new byte[4];
	}
}
